import Game from "../game/Game";
import { Client, Room, SocketResponse, UserData } from "../interface";
import fs from 'fs';
import path from 'path';
export const Globel = {
    rooms:new Map<string,Room>(),
}

//广播封装
export function broadCast(obj:SocketResponse,playerClients:Map<string,Client>){
    playerClients.forEach(i=>{
        i.ctx!.websocket.send(JSON.stringify(obj))
    })
}

//设置玩家分数
export async function setUserScore(obj:{[id:string]:number}){
    let str = await fs.readFileSync(path.resolve(__dirname,"../static/userdata.json")).toString();
    let userdata:{[id:string]:UserData} = JSON.parse(str);
    for(let i in obj){
        userdata[i].scoreNum=obj[i];
    }
    await fs.writeFileSync(path.resolve(__dirname,"../static/userdata.json"),JSON.stringify(userdata));
}